package ar.uba.fi.taller2.ftrs.parser;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.ArrayList;

public class ListaPalabras {
	private Hashtable<String,Palabra> palabras;
	Iterator<String> iterador ;
	
	public ListaPalabras() {
		super();
		this.palabras = new Hashtable<String,Palabra>();
		this.iterador =null;
	}
	
	public boolean agregarPalabra(String Palabra, int valor, int id){
		if (this.palabras.put(Palabra,new Palabra(Palabra,valor,id))!=null) return true;
		else return false;
	}
	protected Hashtable<String,Palabra> getHash(){
	return this.palabras;	
	}
	
	public void sumarOcurrencia(String inputString){
	// A este mètodo se le pasa un string que consiste en una palabra.
	// Si esta palabra se encuentra en la lista de palabras, se le suma
	// 1 a la ocurrencia, si no existe se agraga a la lista.
	if (inputString!=null)				
	       if (this.palabras.containsKey(inputString))
	       {	    	   
	    	   ((Palabra)this.palabras.get(inputString)).setPeso(((Palabra)this.palabras.get(inputString)).getPeso()+1);
	       }else this.palabras.put(inputString,new Palabra(inputString,1,0));	   	
	}
		
	public String next(){
		Palabra P;
		if (this.iterador == null) 
			this.iterador = this.getHash().keySet().iterator();
		if (this.iterador.hasNext()) {		    	 
	    	 P = (Palabra)this.getHash().get(this.iterador.next());	    		    	 
	        }else{
	         this.iterador = null;
	         P = null;
	         return null;
	        }	        	
		return P.getString();
	}
	
	public ArrayList<String> getList(){
		ArrayList<String> retorno = new ArrayList<String>(); 
		Iterator<String> itr= this.palabras.keySet().iterator();	     
		while (itr.hasNext()) {    	 
			Palabra P = (Palabra)this.palabras.get(itr.next());
			retorno.add(P.getString());
	        }	
		return retorno;
	}
	
	public Object[] getArray(){
		ArrayList<String> a = getList();			
		return a.toArray();
	}
	
	double getvalor(String P){
		if (this.palabras.containsKey(P.trim())){
		return ((Palabra)(this.palabras.get(P.trim()))).getPeso();	
		}
		else return 0;
	}

	public void imprimirLista(){
		Iterator<String> itr= this.palabras.keySet().iterator();	     
		 while (itr.hasNext()) {		    	 
	    	 Palabra P = (Palabra)this.palabras.get(itr.next());
	    	 System.out.print("--"+P.getId()+" : "+P.getString()+" : "+P.getPeso()+"\n");
	        }
		
	}
	
}

